package com.advert.model.security;

import com.advert.model.BusinessEntityBase;

import javax.persistence.*;
import java.util.Date;

import static javax.persistence.CascadeType.*;

@Entity
@Table(name = "user")
@NamedQueries({
@NamedQuery(name = "UserEntity.getByLogin", query = "select u from UserEntity u where u.login = :login"),
@NamedQuery(name = "UserEntity.getByLoginAndMail", query = "select u from UserEntity u where u.login = :login and u.userInfo.email = :email"),
@NamedQuery(name = "UserEntity.getById", query = "select u from UserEntity u where u.id = :id"),
@NamedQuery(name = "UserEntity.getByRole", query = "select u from UserEntity u where u.role = :role")
})
public class UserEntity extends BusinessEntityBase {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "login")
    private String login;

    @Column(name = "password")
    private String password;

    @Column(name = "role")
    @Enumerated(EnumType.STRING)
    private Role role;

    @Column(name = "status")
    @Enumerated(EnumType.STRING)
    private Status status;

    @Column(name = "login_attempts")
    private Integer loginAttempts;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "last_login_date")
    private Date lastLoginDate;

    @Column(name = "activation_uid")
    private String activationUid;

    @OneToOne(fetch = FetchType.EAGER, cascade = {PERSIST}, mappedBy = "user")
    private UserInfoEntity userInfo;


    public UserEntity() {
    }

    public UserEntity(String login, String password, Role role, Status status) {
        this.login = login;
        this.password = password;
        this.role = role;
        this.status = status;
    }


    public Integer getId() {
        return id;
    }

    public String getLogin() {
        return login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public Status getStatus() {
        return status;
    }

    public Integer getLoginAttempts() {
        return loginAttempts;
    }

    public void setLoginAttempts(Integer loginAttempts) {
        this.loginAttempts = loginAttempts;
    }

    public void setUserInfo(UserInfoEntity userInfo) {
        if (userInfo != null) {
            userInfo.setUser(this);
        }
        this.userInfo = userInfo;
    }

    public Date getLastLoginDate() {
        return lastLoginDate;
    }

    public void setLastLoginDate(Date lastLoginDate) {
        this.lastLoginDate = lastLoginDate;
    }

    public String getActivationUid() {
        return activationUid;
    }

    public void setActivationUid(String activationUid) {
        this.activationUid = activationUid;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public UserInfoEntity getUserInfo() {
        return userInfo;
    }
}
